import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

import 'InheritedWidgetDemo.dart';
import 'ProvideDemo.dart';
import 'change_notifier_use.dart';

class FuntionWidgetTestRoute extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Material(
      child: SingleChildScrollView(
        child: Column(
          children: [
            TextButton(
              child: Text("跳转自定义的DoubleClick按钮"),
              onPressed: () {
              var result =  Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return WillPopScopeTestRoute();
                }));
              print("跳转自定义的DoubleClick按钮返回值：$result");
              },
            ),

            TextButton(
              child: Text("测试InheritedWidget-根 widget 中通过InheritedWidget共享了一个数据，那么我们便可以在任意子widget 中来获取该共享的数据"),
              onPressed: () {
              var result =  Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return InheritedWidgetTestRoute();
                }));
              },
            ),

            TextButton(
              child: Text("ProviderRoute 自定义一个通用的跨路由的数据架子"),
              onPressed: () {
              var result =  Navigator.push(context, MaterialPageRoute(builder: (context) {
                  return ProviderRoute();
                }));
              },
            ),

            TextButton(
              child: Text("ChangeNotifier监听数据变动的控件"),
              onPressed: () {
                var result = Navigator.push(context,
                    MaterialPageRoute(builder: (context) {
                      return ChangeNotifierTest();
                    }));
              },
            ),
          ],
        ),
      ),
    );
  }
}

class WillPopScopeTestRoute extends StatefulWidget {
  @override
  WillPopScopeTestRouteState createState() {
    return WillPopScopeTestRouteState();
  }
}

class WillPopScopeTestRouteState extends State<WillPopScopeTestRoute> {
  DateTime? _lastPressedAt; //上次点击时间

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        if (_lastPressedAt == null ||
            DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 1)) {
          //两次点击间隔超过1秒则重新计时
          _lastPressedAt = DateTime.now();
          return false;
        }
        return true;
      },
      child: Container(
        alignment: Alignment.center,
        child: TextButton(
          child: Text("点击两次，一秒内"),
          onPressed: () {
            doubleClick();

          },
        ),
      ),
    );
  }

  doubleClick() async {
    if (_lastPressedAt == null ||
        DateTime.now().difference(_lastPressedAt!) > Duration(seconds: 1)) {
      //两次点击间隔超过1秒则重新计时
      _lastPressedAt = DateTime.now();
    }else{
      Navigator.pop(context,"跳转自定义的DoubleClick按钮返回值");
    }
  }
}
